package br.com.threads;

public class Tank {
	private boolean isEmpty = true;

	public synchronized void emptying() {
		pause(true);
		isEmpty = !isEmpty;
		System.out.println("emptying");
		notify();
	}

	public synchronized void filling() {
		pause(false);
		isEmpty = !isEmpty;
		System.out.println("filling");
		notify();
	}

	private void pause(boolean flag) {
		while (flag ? isEmpty : !isEmpty) {
			try {
				wait();
			} catch (InterruptedException ie) {
				System.out.println(Thread.currentThread() + " interrupted.");
			}
		}
	}

	public static void main(String[] args) {
		final Tank token = new Tank();

		(new Thread("A") {
			public void run() {
				for (int i=0;i<10;i++)
					token.emptying();
			}
		}).start();
		
		System.out.println("inicio");
		
		(new Thread("B") {
			public void run() {
				for (int i=0;i<10;i++)
					token.filling(); 
			}
		}).start();
		


	}
}